#include "GameEngine.h"
#include "GameState.h"

GameEngine::GameEngine(void)
{
}

//Game Initialization
void GameEngine::Init(const char* sTitle,int width /* = 1280 */,int height /* = 720 */,bool fullscreen /* = false */)
{
	printf("=======Game Initialization\n");
	_Width = width;
	_Height = height;




	_Window.Create(sf::VideoMode(_Width,_Height),sTitle,sf::Style::Titlebar|sf::Style::Resize,sf::ContextSettings(32,4,4));

	_Window.SetActive();

	printf("OpenGL Version: %s\n",glGetString(GL_VERSION));
	printf("OpenGL GLSL Version: %s\n",glGetString(GL_SHADING_LANGUAGE_VERSION));
	printf("OpenGL Device: %s\n",glGetString(GL_RENDERER));
	printf("OpenGL Vendor: %s\n",glGetString(GL_VENDOR));


	_Time.Reset();

	bFocused = true;

	_Window.SetActive(true);
	printf("=======Game Initialization Complete\n");
}

//Game De-initialization 
void GameEngine::Cleanup()
{
	while (!states.empty())
	{
		states.back()->Cleanup();
		states.pop_back();
	}
	_Window.Close();
	printf("=======Game Closing\n");
}

//Change Game State
void GameEngine::ChangeState(GameState* state)
{
	if (!states.empty())
	{
		states.back()->Cleanup();
		states.pop_back();
	}
	states.push_back(state);
	states.back()->Init(this);
}

//Add State to State Stack
void GameEngine::PushState(GameState* state)
{
	if (!states.empty())
	{
		states.back()->Pause();
	}
	states.push_back(state);
	states.back()->Init(this);
}

//Remove Last State
void GameEngine::PopState()
{
	if (!states.empty())
	{
		states.back()->Cleanup();
		states.pop_back();
	}

	if (!states.empty())
	{
		states.back()->Resume();
	}
}

//Manage Input and Events
void GameEngine::HandleEvents()
{
	states.back()->HandleEvents(this);
}

//Update Game Logic.
void GameEngine::Update()
{
	if (bFocused == true)
	{

		states.back()->Update(this);
	}
}

//Render Frame
void GameEngine::Draw()
{
	if (bFocused == true)
	{

		states.back()->Draw(this);
	}

}

GameEngine::~GameEngine(void)
{
}
